package com.example.dessert.controller.fore;

import com.example.dessert.annotation.LoginCheck;
import com.example.dessert.controller.BaseController;
import com.example.dessert.entity.User;
import com.example.dessert.service.UserService;
import com.example.dessert.util.ResponsePojo;
import com.example.dessert.util.ResultCode;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @ClassName: IndexController
 * @Description:
 * @Author li
 * @Date 2022/1/4
 * @Version 1.0
 */
@Controller
@RequestMapping("/fore/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
     * 用户信息
     */
    @GetMapping("/info")
    @LoginCheck
    public String info(HttpSession session, Model model) {

        Object userId = session.getAttribute("userId");
        logger.info("userId:{}", userId);

        User user = userService.idQueryUser((Long) userId);
        logger.info("userId query user results :{}", user);

        model.addAttribute("user", user);

        return "fore/user/info";

    }


    /**
     * 修改用户基本信息
     */
    @PostMapping("/saveInfo")
    @LoginCheck
    @ResponseBody
    public ResponsePojo saveInfo(HttpSession session,@RequestParam Map<String, Object> map) {

        logger.info("Modify user information request parameters :{}", map);

        String nickname = (String)map.get("nickname");
        Integer sex = Integer.parseInt(map.get("sex").toString());
        String description = (String)map.get("description");
        Object userId = session.getAttribute("userId");

        User users = new User();
        users.setId((Long) userId)
             .setNickname(nickname)
             .setSex(sex)
             .setDescription(description);
        int result = userService.saveUser(users);

        logger.info("Modify user information result :{}", result);

        return ResponsePojo.ok();

    }

    /**
     * 账号设置
     */
    @GetMapping("/settings")
    @LoginCheck
    public String settings(HttpSession session, Model model) {

        Object userId = session.getAttribute("userId");
        logger.info("userId:{}", userId);

        User user = userService.idQueryUser((Long) userId);
        logger.info("userId query user results :{}", user);

        model.addAttribute("user", user);

        return "fore/user/settings";

    }


    /**
     * 修改用户登录密码
     */
    @GetMapping("/savePassword")
    @LoginCheck
    public String savePassword() {

        return "fore/user/save_password";

    }


    /**
     * 修改用户登录密码
     */
    @PostMapping("/savePassword")
    @LoginCheck
    @ResponseBody
    public ResponsePojo savePassword(HttpSession session,@RequestParam Map<String, Object> map) {

        logger.info("Modify user password request parameters :{}", map);

        String oldPassword = (String)map.get("oldPassword");
        String newPassword = (String)map.get("newPassword");
        Object userId = session.getAttribute("userId");
        User user = userService.idQueryUser((Long) userId);

        if(!StringUtils.equals(oldPassword,user.getPassword())){
            return ResponsePojo.error(ResultCode.EXP_INVALID_ACCOUNT,"原密码错误");
        }

        User users = new User();
        users.setId((Long) userId)
                .setPassword(newPassword);
        int result = userService.saveUser(users);

        logger.info("Modify user password result :{}", result);

        return ResponsePojo.ok();

    }



    /**
     * 退出登录
     */
    @PostMapping("/logout")
    @LoginCheck
    @ResponseBody
    public ResponsePojo logout(HttpSession session, SessionStatus sessionStatus) {

        Object userId = session.getAttribute("userId");
        logger.info("User logout userId :{}", userId);

        session.removeAttribute("userId");
        sessionStatus.setComplete();
        return ResponsePojo.ok();

    }



}
